"======================================================================
"
" init-plugin-coc.vim - Coc语言服务器配置
"
"======================================================================


"----------------------------------------------------------------------
" 基本配置
"----------------------------------------------------------------------
" Coc约束 -- 没设置可能导致文本编辑失败
set hidden

" Coc约束 -- 给显示信息留空间
set cmdheight=1

" 刷新时间
set updatetime=200

" Coc约束
set shortmess+=c


"----------------------------------------------------------------------
" 补全配置
"----------------------------------------------------------------------
" 使用CR 确认补全
inoremap <silent><expr> <CR> pumvisible() ? "\<C-y>" : "\<C-g>u<CR>"
" 下一个补全
inoremap <silent><expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>"
" 上一个补全
inoremap <silent><expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"

" 对 log 文件禁用补全
autocmd FileType log let b:coc_suggest_disable = 1

"----------------------------------------------------------------------
" 功能配置
"----------------------------------------------------------------------
let g:coc_enable_locationlist = 0
" 下一个占位符
let g:coc_snippet_next = '<C-l>'
" 上一个占位符
let g:coc_snippet_prev = '<C-h>'

" 格式化代码
xmap <Leader>F  <Plug>(coc-format-selected)
nmap <Leader>F  <Plug>(coc-format-selected)

" 支持json注释
autocmd FileType json syntax match Comment +\/\/.\+$+

" 显示当前buffer下的错误信息
nmap <silent> <Leader>el :CocList diagnostics<CR>
" 下一个错误
nmap <silent> <Leader>en <Plug>(coc-diagnostic-next)
" 上一个错误
nmap <silent> <Leader>em <Plug>(coc-diagnostic-prev)
" 转到定义
nmap <silent> <Leader>td <Plug>(coc-definition)
" 转到类型定义
nmap <silent> <Leader>ty <Plug>(coc-type-definition)
" 转到实现
nmap <silent> <Leader>ti <Plug>(coc-implementation)
" 转到声明
nmap <silent> <Leader>tc <Plug>(coc-declaration)
" 转到引用
nmap <silent> <Leader>tr <Plug>(coc-references)


"----------------------------------------------------------------------
" 多光标配置
"----------------------------------------------------------------------
" 在当前位置放下光标
nmap <silent> <C-i> <Plug>(coc-cursors-position)
" 在选中位置放下光标
xmap <silent> <C-i> <Plug>(coc-cursors-range)

" 选中相同的单词
nmap <expr> <silent> <C-;> <SID>select_current_word()
function! s:select_current_word()
  if !get(g:, 'coc_cursors_activated', 0)
    return "\<Plug>(coc-cursors-word)"
  endif
  return "*\<Plug>(coc-cursors-word):nohlsearch\<CR>"
endfunc
